#Boa:Dialog:LArtSucursal

import wx
import consultasBD

def create(parent):
    return LArtSucursal(parent)

[wxID_LARTSUCURSAL, wxID_LARTSUCURSALBTN_LISTAR, 
 wxID_LARTSUCURSALCODIGO_SUSSAN, wxID_LARTSUCURSALCOMBOCOLOR, 
 wxID_LARTSUCURSALCOMBOGRUPO, wxID_LARTSUCURSALCOMBOTALLE, 
 wxID_LARTSUCURSALDESCRIPCION, wxID_LARTSUCURSALESTACION, 
 wxID_LARTSUCURSALINPROVEEDOR, wxID_LARTSUCURSALPTC, 
 wxID_LARTSUCURSALSTATICLINE1, wxID_LARTSUCURSALSTATICTEXT1, 
 wxID_LARTSUCURSALSTATICTEXT2, wxID_LARTSUCURSALSTATICTEXT3, 
 wxID_LARTSUCURSALSTATICTEXT4, wxID_LARTSUCURSALSTATICTEXT5, 
 wxID_LARTSUCURSALSTATICTEXT6, wxID_LARTSUCURSALSTATICTEXT7, 
 wxID_LARTSUCURSALSTATICTEXT8, wxID_LARTSUCURSALSTATICTEXT9, 
] = [wx.NewId() for _init_ctrls in range(20)]

class LArtSucursal(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_LARTSUCURSAL, name=u'LArtSucursal',
              parent=prnt, pos=wx.Point(416, 268), size=wx.Size(453, 268),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'Listado de articulos')
        self.SetClientSize(wx.Size(437, 232))

        self.codigo_sussan = wx.TextCtrl(id=wxID_LARTSUCURSALCODIGO_SUSSAN,
              name='codigo_sussan', parent=self, pos=wx.Point(292, 27),
              size=wx.Size(120, 21), style=0, value=u'')

        self.descripcion = wx.TextCtrl(id=wxID_LARTSUCURSALDESCRIPCION,
              name=u'descripcion', parent=self, pos=wx.Point(84, 59),
              size=wx.Size(252, 21), style=0, value=u'')

        self.btn_listar = wx.Button(id=wxID_LARTSUCURSALBTN_LISTAR,
              label=u'Listar', name=u'btn_listar', parent=self,
              pos=wx.Point(302, 194), size=wx.Size(85, 27), style=0)
        self.btn_listar.Bind(wx.EVT_BUTTON, self.OnBtn_listarButton,
              id=wxID_LARTSUCURSALBTN_LISTAR)

        self.staticText8 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT8,
              label=u'Color', name=u'staticText8', parent=self, pos=wx.Point(12,
              124), size=wx.Size(33, 15), style=0)

        self.staticText7 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT7,
              label=u'Listado de articulos', name='staticText7', parent=self,
              pos=wx.Point(12, 3), size=wx.Size(124, 15), style=0)

        self.staticText6 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT6,
              label=u'Grupo', name='staticText6', parent=self, pos=wx.Point(236,
              123), size=wx.Size(39, 15), style=0)

        self.staticText5 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT5,
              label=u'Talle', name='staticText5', parent=self, pos=wx.Point(12,
              91), size=wx.Size(28, 15), style=0)

        self.staticText4 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT4,
              label=u'Estacion', name='staticText4', parent=self,
              pos=wx.Point(236, 91), size=wx.Size(55, 15), style=0)

        self.staticText3 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT3,
              label=u'Descripcion', name='staticText3', parent=self,
              pos=wx.Point(12, 59), size=wx.Size(74, 15), style=0)

        self.staticText1 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT1,
              label='Cod. Sussan', name='staticText1', parent=self,
              pos=wx.Point(228, 27), size=wx.Size(61, 13), style=0)

        self.staticLine1 = wx.StaticLine(id=wxID_LARTSUCURSALSTATICLINE1,
              name='staticLine1', parent=self, pos=wx.Point(12, 19),
              size=wx.Size(420, 2), style=0)

        self.staticText2 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT2,
              label='Proveedor:', name='staticText2', parent=self,
              pos=wx.Point(16, 160), size=wx.Size(55, 13), style=0)

        self.inProveedor = wx.TextCtrl(id=wxID_LARTSUCURSALINPROVEEDOR,
              name='inProveedor', parent=self, pos=wx.Point(88, 160),
              size=wx.Size(120, 21), style=0, value='')

        self.staticText9 = wx.StaticText(id=wxID_LARTSUCURSALSTATICTEXT9,
              label='PTC', name='staticText9', parent=self, pos=wx.Point(16,
              24), size=wx.Size(20, 13), style=0)

        self.ptc = wx.TextCtrl(id=wxID_LARTSUCURSALPTC, name='ptc', parent=self,
              pos=wx.Point(80, 24), size=wx.Size(120, 21), style=0, value='')

        self.comboGrupo = wx.ComboBox(choices=[],
              id=wxID_LARTSUCURSALCOMBOGRUPO, name='comboGrupo', parent=self,
              pos=wx.Point(296, 120), size=wx.Size(130, 21), style=0, value='')
        self.comboGrupo.SetLabel('')

        self.comboColor = wx.ComboBox(choices=[],
              id=wxID_LARTSUCURSALCOMBOCOLOR, name='comboColor', parent=self,
              pos=wx.Point(88, 120), size=wx.Size(130, 21), style=0, value='')
        self.comboColor.SetLabel('')

        self.comboTalle = wx.ComboBox(choices=[],
              id=wxID_LARTSUCURSALCOMBOTALLE, name='comboTalle', parent=self,
              pos=wx.Point(88, 88), size=wx.Size(130, 21), style=0, value='')
        self.comboTalle.SetLabel('')

        self.estacion = wx.ComboBox(choices=["", "Verano", "Primavera", "Otono",
              "Invierno"], id=wxID_LARTSUCURSALESTACION, name='estacion',
              parent=self, pos=wx.Point(296, 88), size=wx.Size(128, 21),
              style=0, value='')
        self.estacion.SetLabel('')

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.padrepadre= parent
        self.sucursalNro = parent.sucursalNro
        self.llenaCombos()
        #self.campos = [self.codigo_barra, self.sucursal, self.descripcion,self.estacion, self.talle, self.color, self.grupo]


    def llenaCombos(self):
        #GRUPOS
        list = []
        condicionSQL = "1"
        resultados = consultasBD.executaSQL(self,"*","grupos",condicionSQL)
        for r in resultados:
                list.append(""+str(r[0])+" -- "+str(r[1])+"")
        
        self.comboGrupo.SetItems(list)
        
        #COLORES
        list2 = []
        condicionSQL2 = "1"
        resultados2 = consultasBD.executaSQL(self,"*","colores",condicionSQL2)
        for r in resultados2:
                list2.append(""+str(r[0])+" -- "+str(r[1])+"")
        
        self.comboColor.SetItems(list2)
        
        
        
        #Talles
        list3 = []
        condicionSQL3 = "1 "
        resultados3 = consultasBD.executaSQL(self,"DISTINCT(talle)","producto_talle_color ",condicionSQL3)
        for r in resultados3:
                list3.append(str(r[0]))
        
        self.comboTalle.SetItems(list3)
        
        #
        
        
        
    def OnBtn_listarButton(self, event):
        import Vista
        import time
        
        ptc = "AND a.id= '%s'" % str(self.ptc.Value)
        if str(self.ptc.Value) == "":
            ptc = ""
            
        provee = "AND b.proveedor = '%s'" % str(self.inProveedor.Value)
        if str(self.inProveedor.Value) == "":
            provee = ""
        
        estacion = self.estacion.Value 
        print "ESSTAAAAAAAAACION", estacion
        if estacion=="":
            print "EEEEEEEEEEEEEEEEEEEENNNNTE"
            estacion="%%"
        
        grupo = self.comboGrupo.Value.split(" -- ")[0]
        if grupo=="":
            grupo = "%%"
            
        color = self.comboColor.Value.split(" -- ")[0]
        if color=="":
            color = "%%"            
        
        codsussan = "AND a.codsussan like'%%%s%%' " % str(self.codigo_sussan.GetValue())
        
        SQL = "SELECT distinct a.id,a.codsussan,a.talle,a.color,col.nombre,b.descri,b.preciolt1,b.grupo,\
               COALESCE(c.stock, 0) as SANTAFE, COALESCE(d.stock, 0) as PARANA \
               FROM colores col , producto_talle_color a JOIN productos b ON (a.codsussan=b.codsussan) LEFT JOIN stock c ON (a.id=c.ptc AND c.sucursal = 1) LEFT JOIN stock d ON (a.id=d.ptc AND d.sucursal = 2 ) \
               WHERE col.idcolor=a.color and (a.talle LIKE '%%%s%%') AND (a.color LIKE '%%%s%%') AND (b.descri like '%%%s%%') %s AND b.estacion like '%s' AND b.grupo LIKE '%s' %s %s " % (str(self.comboTalle.Value),str(color),str(self.descripcion.Value),str(provee),str(estacion),str(grupo),ptc,codsussan)
        
        
        print SQL
        print "EEMPEZO EL SQL", time.time()
        datos = consultasBD.ordenCruda(SQL)
        
        print "TEEEERMINO EL SQL", time.time()
        
        #levantar de la base de datos
        columnas = ['PTC', 'Cod. Sussan', 'Talle','Color ','COLOR DESCR.','Descrip.','P.Venta','Grupo','Stock Sta Fe','Stock Parana']
        vista = Vista.Vista(self, 'Busqueda de Articulos')
        vista.cargar_celdas(datos, columnas)
        vista.ShowModal()
        event.Skip()
